package swingTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButtonWithImg {

    public static void main(String[] args) throws AWTException {
        JFrame jf = new JFrame("测试窗口");
        jf.setSize(1400, 700);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLayout(null);
        final JButton btn = new JButton();

        btn.setSize(200, 78);
        // 设置按钮的默认图片
        btn.setIcon(new ImageIcon("images/1.png"));

        // 设置按钮被点击时的图片
        btn.setPressedIcon(new ImageIcon("images/2.png"));

        // 不绘制边框
        btn.setBorderPainted(false);
        btn.setContentAreaFilled(false);    //不绘制默认按钮背景
        btn.setFocusPainted(false);           //不绘制图片或文字周围的焦点虚框
        // 添加按钮点击事件监听器
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按钮被点击了");

            }
        });

        jf.add(btn);


        ImageIcon imageIcon = new ImageIcon("images/menu_bg.jpg");
        Image image = imageIcon.getImage();
        JLabel label = new JLabel(imageIcon);
        label.setBounds(0,0,1400,700);
        btn.setBounds(600,400,200,74);
        jf.add(label);
        jf.setVisible(true);
    }


}
